package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.category.pojo.Category;
import com.hmall.common.PageBean;
import com.hmall.item.mapper.SearchMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.SearchService;
import com.hmall.search.dto.SearchDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SearchServiceImp implements SearchService {

    @Autowired
    SearchMapper searchMapper;

    @Override
    public PageBean<Item> filterSearch(Integer page, Integer size, String sortBy, String category, String brand) {
        if (sortBy.equals("default")){
            if (category != null && brand != null) {
                Page<Item> p = new Page<>(page, size);
                QueryWrapper wrapper = Wrappers.query().eq("category", brand).eq("brand",brand).orderByDesc("create_time", "update_time", "id");

                searchMapper.selectPage(p, wrapper);
                System.out.println(1);
            } else if (category != null) {
                Page<Item> p = new Page<>(page, size);
                QueryWrapper wrapper = Wrappers.query().eq("category", category).orderByDesc("create_time", "update_time", "id");
                searchMapper.selectPage(p, wrapper);
                System.out.println(2);
            } else if (brand != null) {
                Page<Item> p = new Page<>(page, size);
                QueryWrapper wrapper = Wrappers.query().eq("brand",brand).orderByDesc("create_time", "update_time", "id");
                searchMapper.selectPage(p, wrapper);
                System.out.println(3);
            }
            System.out.println("排序方式为默认");
    }
        else if(sortBy.equals("price"))

        {
            if (category != null && brand != null) {
                Page<Item> p = new Page<>(page, size);
                QueryWrapper wrapper = Wrappers.query().eq("category", category).eq("brand",brand).orderByDesc("price", "id");

                searchMapper.selectPage(p, wrapper);
                System.out.println(4);
            } else if (category != null) {
                Page<Item> p = new Page<>(page, size);
                QueryWrapper wrapper =Wrappers.query().eq("category", category).orderByDesc("price", "id");
                searchMapper.selectPage(p, wrapper);
                System.out.println(5);
            } else if (brand != null) {
                Page<Item> p = new Page<>(page, size);
                QueryWrapper wrapper = Wrappers.query().eq("brand",brand).orderByDesc("price", "id");
                searchMapper.selectPage(p, wrapper);
                System.out.println(6);
            }
            System.out.println("排序方式为价格");
        }
        else if(sortBy.equals("sold"))

        {
            if (category != null && brand != null) {
                Page<Item> p = new Page<>(page, size);
                QueryWrapper wrapper = Wrappers.query().eq("category", category).eq("brand",brand).orderByDesc("sold", "id");

                searchMapper.selectPage(p, wrapper);
                System.out.println(7);
            } else if (category != null) {
                Page<Item> p = new Page<>(page, size);
                QueryWrapper wrapper = Wrappers.query().eq("category", category).orderByDesc("sold", "id");
                searchMapper.selectPage(p, wrapper);
                System.out.println(8);
            } else if (brand != null) {
                Page<Item> p = new Page<>(page, size);
                QueryWrapper wrapper = Wrappers.query().eq("brand",brand).orderByDesc("sold", "id");
                searchMapper.selectPage(p, wrapper);
                System.out.println(9);
            }
            System.out.println("排序方式为销量");
        }
        return null;

}
}



